package net.adetech.crj.web;


import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Mod;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    ServiceUtil serviceUtil;


    @Value("${wgrMaintenance}")
    public static boolean wgrMaintenance = false;

    @RequestMapping("/index")
    public String index(HttpSession session, Model model, String IdNum, String RealName, String AcountId,String WxOpenId, double lat, double lng) {

        if(WxOpenId!=null)
        {
            AcountId = decrypt(AcountId);
            RealName = decrypt(RealName);
            IdNum = decrypt(IdNum);
        }
        User user = this.handleUser(session, IdNum, RealName, AcountId, lat, lng,null);
        return "userType";
    }


    @RequestMapping("/zggmIndex")
    public String zggmIndex(HttpSession session, Model model, String IdNum, String RealName, String AcountId,String WxOpenId, double lat, double lng) {

        if(WxOpenId!=null)
        {
            AcountId = decrypt(AcountId);
            RealName = decrypt(RealName);
            IdNum = decrypt(IdNum);
        }
        User user = this.handleUser(session, IdNum, RealName, AcountId, lat, lng,"1");
        model.addAttribute("userJson", "var currentUser = " + JSON.toJSONString(user) + ";");
        return "index";
    }

    @RequestMapping("/wgrIndex")
    public String wgrIndex(HttpSession session, Model model, String IdNum, String RealName, String AcountId,String WxOpenId, double lat, double lng) {

        if (wgrMaintenance)
        {
            return "404";
        }
        if(WxOpenId!=null)
        {
            AcountId = decrypt(AcountId);
            RealName = decrypt(RealName);
            IdNum = decrypt(IdNum);
        }
        User user = this.handleUser(session, IdNum, RealName, AcountId, lat, lng,"2");
        model.addAttribute("userJson", "var currentUser = " + JSON.toJSONString(user) + ";");
        return "wgrIndex";
    }

    @RequestMapping("/userType")
    public String userType(HttpSession session, Model model, String t) {


        if ("2".equals(t)&&wgrMaintenance)
        {
            return "404";
        }

        User user = (User) session.getAttribute("currentUser");
        if (user != null) {
            user.setUserType(t);
            if(user.getIdNumber()!=null&&!"".equals(user.getIdNumber()))
            {
                serviceUtil.callService("user.login", "user", user);
            }
        }
        model.addAttribute("userJson", "var currentUser = " + JSON.toJSONString(user) + ";");
        if ("1".equals(t)) {
            return "index";
        } else {
            return "wgrIndex";
        }

    }




    private User handleUser(HttpSession session, String idNumber, String realName, String phoneNumber, double lat, double lng,String userType) {


        User user = new User();
        session.setAttribute("currentUser", user);
        user.setLat(lat);
        user.setLng(lng);
        if (idNumber != null && !"".equals(idNumber)) {
            user.setIdNumber(idNumber);
            user.setRealName(realName);
            user.setPhoneNumber(phoneNumber);
            if(userType!=null)
            {
                user.setUserType(userType);
            }
            ServiceResponse response = serviceUtil.callService("user.login", "user", user);
            User tempUser = (User) response.get("user");
            user.setUserType(tempUser.getUserType());
        }

        return user;

    }

    private static String key = "58563214";
    private static String key1 = "32128158";

    public static String decrypt(String message) {
        try {
//            byte[] bytesrc = new BASE64Decoder().decodeBuffer(message);
            byte[] bytesrc = Base64.getDecoder().decode(message);

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key1.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            String result = new String(retByte,"UTF-8");
            System.out.println("decrypt:" + message+"-->" +result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
